package org.chnxi.study.leetcode.questions25.ques3;

/**
 * 无重复字符的最长子串-使用暴力方式双层循环逐一尝试
 * 执行用时 :385 ms, 在所有 Java 提交中击败了5.67%的用户
 * 内存消耗 :39.9 MB, 在所有 Java 提交中击败了5.20%的用户
 */
public class Simple1 {

    public static void main(String[] args) {
        System.out.println("abcabcbb："+lengthOfLongestSubstring("abcabcbb"));
        System.out.println("bbbbb："+lengthOfLongestSubstring("bbbbb"));
        System.out.println("pwwkew："+lengthOfLongestSubstring("pwwkew"));
    }

    public static int lengthOfLongestSubstring(String s) {
        if(null == s || "".equals(s = s.toString())){
            return 0;
        }
        int maxLen = 1;

        for (int i = 0; i < s.length(); i++){
            int len = 1;
            for (int j = i+1; j < s.length(); j++){
                if(!s.substring(i,j).contains(s.substring(j , j+1))){
                    len++;
                    if(maxLen < len){
                        maxLen = len;
                    }
                }else{
                    len = 1;
                    break;
                }
            }
        }
        return maxLen;
    }

}
